package com.laowang.project.system.controller;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ExecutionException;

import com.laowang.common.exception.CustomException;
import com.laowang.common.utils.MobileCheck;
import com.laowang.common.utils.PayUtil;
import com.laowang.project.pay.config.MyWxPayConfigUtil;
import com.laowang.project.system.domain.vo.AppUserSub;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.laowang.framework.aspectj.lang.annotation.Log;
import com.laowang.framework.aspectj.lang.enums.BusinessType;

import org.springframework.web.bind.annotation.*;
import com.laowang.project.system.domain.TbChargeSystem;
import com.laowang.project.system.service.TbChargeSystemService;
import com.laowang.framework.web.controller.BaseController;
import com.laowang.framework.web.domain.AjaxResult;
import com.laowang.common.utils.poi.ExcelUtil;
import com.laowang.framework.web.page.TableDataInfo;

import javax.validation.constraints.NotBlank;

/**
 * 收费系统Controller
 * 
 * @author laowang
 * @date 2023-02-14
 */
@Api(tags = "收费系统接口")
@RestController
@RequestMapping("/charge")
public class TbChargeSystemController extends BaseController {

    @Autowired
    private TbChargeSystemService tbChargeSystemService;




    @GetMapping("/queryByPhone")
    public AjaxResult queryByPhone(String phone) {


        return AjaxResult.success("成功");
    }



    /**
     * 查询收费系统列表
     */
    @ApiOperation(value = "前端支付",notes = "")
    @PostMapping("/addOrder")
    public AjaxResult addOrder(@RequestBody TbChargeSystem tbChargeSystem) {
        BigDecimal amount = tbChargeSystem.getAmount();
        if (amount.compareTo(new BigDecimal(0)) <=0){
            return AjaxResult.success("支付的金额有误");
        }
        Integer type = tbChargeSystem.getType();
        if (Objects.isNull(type)){
            throw new CustomException("支付的类型为空");
        }
        if (!type.equals(1) && !type.equals(2)){
            return AjaxResult.success("支付的类型不匹配");
        }

        if (type.equals(1)){
            //微信
            MyWxPayConfigUtil myWxPayConfigUtil =new MyWxPayConfigUtil("","800525000005566","9B768CBC1033522F508E1EEDAEE5FC5C","84760","88272641",new Date(),"","562","1234594985","1","1","");
            try {
                Map<String, String> map = PayUtil.test4(myWxPayConfigUtil);
                System.out.println("map:"+map);
            } catch (InterruptedException | IOException | ExecutionException e) {
                e.printStackTrace();
            }

        }else if (type.equals(2)){
            //支付宝
            MyWxPayConfigUtil myWxPayConfigUtil =new MyWxPayConfigUtil();
            try {
                Map<String, String> map = PayUtil.test6(myWxPayConfigUtil);
                System.out.println("map:"+map);
            } catch (InterruptedException | ExecutionException | IOException e) {
                e.printStackTrace();
            }
        }




        return AjaxResult.success();
    }


    /**
     * 查询收费系统列表
     */
    @ApiOperation(value = "查询收费系统列表",notes = "")
    @PreAuthorize("@ss.hasPermi('system:system:list')")
    @GetMapping("/list")
        public TableDataInfo list(TbChargeSystem tbChargeSystem) {
        startPage();
        List<TbChargeSystem> list = tbChargeSystemService.selectTbChargeSystemList(tbChargeSystem);
        return getDataTable(list);
    }
    
    /**
     * 导出收费系统列表
     */
    @ApiOperation(value = "导出收费系统列表",notes = "")
    @PreAuthorize("@ss.hasPermi('system:system:export')")
    @Log(title = "收费系统", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TbChargeSystem tbChargeSystem) {
        List<TbChargeSystem> list = tbChargeSystemService.selectTbChargeSystemList(tbChargeSystem);
        ExcelUtil<TbChargeSystem> util = new ExcelUtil<TbChargeSystem>(TbChargeSystem.class);
        return util.exportExcel(list, "system");
    }

    /**
     * 获取收费系统详细信息
     */
    @ApiOperation(value = "获取收费系统详细信息",notes = "")
    @PreAuthorize("@ss.hasPermi('system:system:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(tbChargeSystemService.selectTbChargeSystemById(id));
    }

    /**
     * 新增收费系统
     */
    @ApiOperation(value = "新增收费系统",notes = "")
    @PreAuthorize("@ss.hasPermi('system:system:add')")
    @Log(title = "收费系统", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbChargeSystem tbChargeSystem) {
        return toAjax(tbChargeSystemService.insertTbChargeSystem(tbChargeSystem));
    }

    /**
     * 修改收费系统
     */
    @ApiOperation(value = "修改收费系统",notes = "")
    @PreAuthorize("@ss.hasPermi('system:system:edit')")
    @Log(title = "收费系统", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbChargeSystem tbChargeSystem) {
        return toAjax(tbChargeSystemService.updateTbChargeSystem(tbChargeSystem));
    }

    /**
     * 删除收费系统
     */
    @ApiOperation(value = "删除收费系统",notes = "")
    @PreAuthorize("@ss.hasPermi('system:system:remove')")
    @Log(title = "收费系统", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(tbChargeSystemService.deleteTbChargeSystemByIds(ids));
    }
}
